<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyValue" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search()">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.employee" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="username" label="员工姓名" width="120" sortable>
			</el-table-column>
			<el-table-column prop="email" label="邮箱" width="220"  sortable>
			</el-table-column>
			<el-table-column prop="headImage" label="头像" width="200" sortable>
			</el-table-column>
			<el-table-column prop="age" label="年龄" width="100" sortable>
			</el-table-column>
			<el-table-column prop="department.name" label="所属部门"width="180" sortable>
			</el-table-column>
			<el-table-column label="操作"  min-width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
					<el-button type="primary" size="small" @click="handleRole(scope.$index, scope.row)">职位设定</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
						   :current-page="query.currentPage" :page-size="query.pageSize" :total="pageInfo.total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="员工姓名" prop="username">
					<el-input v-model="saveForm.username" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="邮箱">
					<el-input v-model="saveForm.email" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="头像">
					<el-input v-model="saveForm.headImage" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="年龄">
					<el-input  v-model="saveForm.age" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="上级部门" prop="parent_id">
					<el-cascader
							v-model="saveForm.department"
							:options="departmentTree"
							:props="{ checkStrictly: true ,
									label:'name',
									value:'id'}"
							clearable></el-cascader>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>

		<el-dialog title="设置职位" :visible.sync="setEmployeeVisible" :close-on-click-modal="false">
			<el-form :model="roleEmployee" label-width="80px" ref="saveMenuForm">
				<el-checkbox :indeterminate="isIndeterminate" v-model="checkEmployeeAll" @change="handleCheckEmployeeAllChange">全选</el-checkbox>
				<div class="checkbox-table" v-for="(p,indexkey) in roleTree" :key="p.id">
					<template>
						<el-checkbox class="check3" style="font-weight: 600;margin-bottom: 15px "
									 v-model='roleEmployee.roleId' :label="p.id"
									 >
							{{p.name}}
						</el-checkbox>
					</template>
				</div>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="setMenuVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveRoleEmployee" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	// import util from '../../common/js/util'
	// //import NProgress from 'nprogress'
	// import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				departmentTree:[],
				pageInfo:{
					employee: [],
					total:0
				},
				query:{
					currentPage:1,
					pageSize:10,
					keyValue:""
				},
				roleEmployee:{
					employeeId:null,
					roleId:[]//记录所有已选定的权限的id的
				},
				total: 0,
				page: 1,
				listLoading: false,
				sels: [],//列表选中列
				roleTree:[],
				checkEmployeeAll:[],
				allRoleId:[],
				saveLoading:false,
				setEmployeeVisible:false,
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				isIndeterminate:false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},

				saveFormVisible: false,//新增界面是否显示
				addLoading: false,
				saveFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//新增或编辑界面数据
				saveForm: {
					username: '',
					email: '',
					headImage: '',
					birth: '',
					age: '',
					department:{
						id:null,
					}
				}

			}
		},
		methods: {
			getDepartmentTree(){
				this.$http.get("/department/tree ").then(result=>{
					result=result.data;
					this.departmentTree=result.object;
				}).catch(()=>{
					this.$message({
						message: '上级部门查询失败',
						type: 'error'
					});
				})
			},
			handleCheckEmployeeAllChange(val){//处理全选
				this.roleEmployee.roleId = val ? this.allRoleId : [];
				this.isIndeterminate = false;
			},
			handleCurrentChange(val) {
				this.query.currentPage = val;
				this.getEmployee();
			},
			search(){
				this.query.currentPage=1;
				this.getEmployee();
			},
			//获取用户列表
			getEmployee() {
				this.$http.post("/employee",this.query).then(result=>{
					result=result.data;
					console.log(this.query," employee query列表显示")
					console.log(result," employee列表显示")
					this.pageInfo.employee=result.object.rows;
					this.pageInfo.total=result.object.size;
				}).catch(()=>{
					this.$message({
						message: '网络波动',
						type: 'error'
					});
				})
			},
			handleRole(index, row){
				this.setEmployeeVisible=true;
				this.getRoleTree();
				this.roleEmployee.employeeId=row.id;
				this.$http.get("/employee/roleId/"+row.id).then(result=>{
					result=result.data;
					if (result.success){
						for (let i=0;i<result.object.length;i++){
							this.roleEmployee.roleId.push(parseInt(result.object[i]))
						}
					}
					console.log(this.roleMenu," this.roleMenu");
				})
			},

			getRoleTree(){
				this.$http.get("/employee/role").then(result=>{
					result=result.data;
					if (result.success){
						this.roleTree=result.object;
						if (this.roleTree&&this.roleTree.length>0){
							for (let i=0;i<this.roleTree.length;i++){
								this.allRoleId.push(this.roleTree[i].id);
							}
						}
					}else {
						this.$message({
							message: '菜单角色树获取失败'+result.message,
							type: 'error'
						});
					}
				})
			},
			saveRoleEmployee(){
				this.$confirm('确认提交吗？', '提示', {}).then(()=>{
					this.saveLoading = true;
					console.log(this.roleTree,"zzzzzzz");
					console.log(this.roleEmployee,"ccccccccc");
					this.$http.put("/employee/role",this.roleEmployee).then(result=>{
						result=result.data;
						console.log(result,"/employee/role")
						this.saveLoading = false;
						this.setEmployeeVisible=false;
						if(result.success){
							this.$message({
								message: '修改成功',
								type: 'success'
							});
						}else {
							console.log(result.message)
							this.$message({
								message: '修改失败'+result.message,
								type: 'error'
							});
						}
					}).catch(()=>{

					})
				})
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					this.$http.delete("/employee/"+row.id).then(result=>{

						result=result.data;
						this.listLoading = false;
						if (result.success){
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.query.currentPage=1;
							this.getEmployee();
						}else {
							this.$message({
								message: '删除失败,请重试',
								type: 'error'
							});
						}
					})
				}).catch(() => {
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.saveFormVisible = true;
				this.saveForm = Object.assign({}, row);
				this.getDepartmentTree();
			},
			//显示新增界面
			handleAdd: function () {
				this.saveFormVisible = true;
				this.saveForm = {
					username: '',
					email: '',
					headImage: '',
					birth: '',
					age: '',
					department:{
						id:null,
					}
				};
				this.getDepartmentTree();
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
							editUser(para).then((res) => {
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getUsers();
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let para=Object.assign({},this.saveForm);
							let val=para.department;
							para.department={id: val[val.length-1]};
							this.$http.put("/employee",para).then(result=>{
								this.saveFormVisible = false;
								this.addLoading = false;
								result=result.data;
								console.log(result," 222222222");
								if (result.success){
									this.$message({
										message: '员工新增成功',
										type: 'success'
									});
									this.getEmployee();
								}else {
									this.$message({
										message: '新增失败',
										type: 'error'
									});
								}
							}).catch(()=>{
								this.$message({
									message: '网络波动',
									type: 'error'
								});
							})
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					this.$http.patch("/employee",ids).then(result=>{
						this.listLoading = false;
						result=result.data;
						if (result.success){
							this.$message({
								message: '批量删除成功',
								type: 'success'
							});
							this.query.currentPage=1;
							this.getEmployee();
						}else {
							this.$message({
								message: '批量删除失败',
								type: 'error'
							});
						}
					})
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getEmployee();
		}
	}

</script>

<style scoped>

</style>